package com.demo.controller;


import com.alibaba.fastjson.JSONObject;
import com.demo.entity.Department;
import com.demo.jwt.JwtTokenUtil;
import com.demo.response.Result;
import com.demo.service.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * 医院科室信息表 前端控制器
 *
 * @author zm
 * @since 2020-11-26
 */
@RestController
@RequestMapping("/department")
@Api(tags = "医院科室类")
public class DepartmentController {
    @Autowired
    IDepartmentService departmentService;
    @Autowired
    IPatientService patientService;
    @Autowired
    private HttpServletRequest request;
    @Autowired
    IVtePreventiveService iVtePreventiveService;
    @Autowired
    IVtePreventiveDoctorAdviceService adviceService;
    @Autowired
    IDvtDiagnoseService dvtDiagnoseService;

    @ApiOperation("新增医院科室信息")
    @PostMapping("/addDepartment")
    public Result addDepartment(@Validated @RequestBody Department department, BindingResult result) throws Exception {
        if (result.hasErrors()) {
            return Result.error(result.getFieldError().getDefaultMessage());
        }
        return departmentService.addDepartment(department);
    }

    @ApiOperation("更新医院科室信息")
    @PostMapping("/updateDepartment")
    public Result updateDepartment(@RequestBody Department department, BindingResult result) throws Exception {
        if (result.hasErrors()) {
            return Result.error(result.getFieldError().getDefaultMessage());
        }
        return departmentService.updateDepartment(department);
    }

    @ApiOperation("删除医院科室信息")
    @GetMapping("/deleteDepartment")
    public Result deleteDepartment(@RequestParam(name = "deptNo", required = true) Integer deptNo,
                                   @RequestParam(name = "deletedUserId", required = true) Integer deletedUserId) {
        return departmentService.deleteDepartment(deptNo, deletedUserId);
    }

    @ApiOperation(value = "医院科室信息列表", tags = {"医院科室类"})
    @GetMapping("/departmentList")
    public Result departmentList(@RequestParam(name = "pageNumber", required = false, defaultValue = "1") Integer pageNumber,
                                 @RequestParam(name = "pageSize", required = false, defaultValue = "10") Integer pageSize,
                                 @RequestParam(name = "类型1内科2外科", required = false) Integer deptType) {

        return departmentService.departmentList(pageNumber, pageSize, deptType);
    }

    @ApiOperation(value = "用户归属科室列表", tags = {"医院科室类", "v20210419更新", "v20210413更新"})
    @GetMapping("/userDepartmentList")
    public Result userDepartmentList(@RequestParam(name = "pageNumber", required = false, defaultValue = "1") Integer pageNumber,
                                     @RequestParam(name = "pageSize", required = false, defaultValue = "10") Integer pageSize) {
        JSONObject jsonObject = JwtTokenUtil.decodeTokenPayload(this.request.getHeader("token"));
        if (jsonObject.containsKey("userId")) {
            Integer userId = (Integer) jsonObject.get("userId");
            return this.departmentService.userDepartmentList(pageNumber, pageSize, userId);
        }
        return Result.error("用户不存在");
    }

    @ApiOperation("科室VTE患者防治监控")
    @GetMapping("/departmentMonitor")
    public Result departmentMonitor(@RequestParam(name = "deptSn", required = false) Integer deptSn) {
        return departmentService.departmentMonitor(deptSn);
    }

    @ApiOperation("科室VTE患者防治监控----患者列表")
    @GetMapping("/vteDepartmentMonitoring")
    public Result vteDepartmentMonitoring(@RequestParam(name = "pageNumber", required = false, defaultValue = "1") Integer pageNumber,
                                          @RequestParam(name = "pageSize", required = false, defaultValue = "10") Integer pageSize,
                                          @RequestParam(name = "deptSn") Integer deptSn) {
        return patientService.vteDepartmentMonitoring(pageNumber, pageSize, deptSn);
    }

    @ApiOperation(value = "vte评估科室排行榜", tags = {"医院科室类", "v20210513更新"})
    @GetMapping({"/vteDepartmentRank"})
    @ApiImplicitParams({@ApiImplicitParam(name = "orderType", value = "排行类型 desc 降序，asc 逆序"), @ApiImplicitParam(name = "topNumber", required = false, value = "排行数，默认 5 条")})
    public Result vteDepartmentRank(@RequestParam(name = "orderType", required = false, defaultValue = "desc") String orderType, @RequestParam(name = "topNumber", required = false, defaultValue = "5") Integer topNumber) throws Exception {
        return this.iVtePreventiveService.getVteDeptSnRanking(orderType, topNumber);
    }

    @ApiOperation(value = "vte出血风险评估率科室排行榜", tags = {"医院科室类", "v20210513更新"})
    @GetMapping({"/vteBleedDepartmentRank"})
    @ApiImplicitParams({@ApiImplicitParam(name = "orderType", value = "排行类型 desc 降序，asc 逆序"), @ApiImplicitParam(name = "topNumber", required = false, value = "排行数，默认 5 条")})
    public Result vteBleedDepartmentRank(@RequestParam(name = "orderType", required = false, defaultValue = "desc") String orderType, @RequestParam(name = "topNumber", required = false, defaultValue = "5") Integer topNumber) throws Exception {
        return this.iVtePreventiveService.getVteBleedDeptSnRanking(orderType, topNumber);
    }

    @ApiOperation(value = "DVT诊断筛查率排行", tags = {"医院科室类", "v20210513更新"})
    @GetMapping({"/getDvtRankByDeptSn"})
    @ApiImplicitParams({@ApiImplicitParam(name = "orderType", value = "排行类型 desc 降序，asc 逆序"), @ApiImplicitParam(name = "topNumber", required = false, value = "排行数，默认 5 条")})
    public Result getDvtRankByDeptSn(@RequestParam(name = "orderType", required = false, defaultValue = "desc") String orderType, @RequestParam(name = "topNumber", required = false, defaultValue = "5") Integer topNumber) throws Exception {
        return this.dvtDiagnoseService.getDvtRankByDeptSn(orderType, topNumber);
    }

    @ApiOperation(value = "dvt预防措施实施率部门排行", tags = {"医院科室类", "v20210513更新"})
    @GetMapping({"/getVteAdviceDeptSnRanking"})
    @ApiImplicitParams({@ApiImplicitParam(name = "orderType", value = "排行类型 desc 降序，asc 逆序"), @ApiImplicitParam(name = "topNumber", required = false, value = "排行数，默认 5 条")})
    public Result getVteAdviceDeptSnRanking(@RequestParam(name = "orderType", required = false, defaultValue = "desc") String orderType, @RequestParam(name = "topNumber", required = false, defaultValue = "5") Integer topNumber) throws Exception {
        return this.adviceService.getVteAdviceDeptSnRanking(orderType, topNumber);
    }

}
